package com.ajax;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.DataBase;

/**
 * Servlet implementation class ItemAmountCheck
 */
@WebServlet("/files/ItemAmountCheck")
public class ItemAmountCheck extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ItemAmountCheck() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		try {
			Connection con = DataBase.getConnection();
			Long iid = Long.parseLong(request.getParameter("iid"));
			Double amount = Double.parseDouble(request.getParameter("amount"));
			PreparedStatement pstmtAmount = con.prepareStatement("SELECT amount FROM item WHERE iid=?");
			pstmtAmount.setLong(1, iid);
			ResultSet rsAmount = pstmtAmount.executeQuery();
			PrintWriter out = response.getWriter();
			if( rsAmount.next() ) {
				if( rsAmount.getDouble(1)<amount )
					out.println("{\"ack\":"+false+"}");
				else
					out.println("{\"ack\":"+true+"}");
			}
			else
				out.println("{\"ack\":"+false+"}");
			rsAmount.close();
			con.close();
		} catch (SQLException e2) {
			e2.printStackTrace();
		}
	}

}
